import cookies from './util.cookies'
import db from './util.db'
import log from './util.log'

const util = {
  cookies,
  db,
  log
}

/**
 * @description 更新标题
 * @param {String} title 标题
 */
util.title = function (titleText) {
  const processTitle = process.env.VUE_APP_TITLE || 'D2Admin'
  window.document.title = `${processTitle}${titleText ? ` | ${titleText}` : ''}`
}

/**
 * @description 打开新页面
 * @param {String} url 地址
 */
util.open = function (url) {
  var a = document.createElement('a')
  a.setAttribute('href', url)
  a.setAttribute('target', '_blank')
  a.setAttribute('id', 'd2admin-link-temp')
  document.body.appendChild(a)
  a.click()
  document.body.removeChild(document.getElementById('d2admin-link-temp'))
}

util.getTree = function (list, idName = 'id', parentIdName = 'parentId', childrenName = 'children') {

  list.forEach(element => {
    let parentId = element[parentIdName];
    if(parentId){
      list.forEach(ele => {
        //当内层循环的ID== 外层循环的parendId时，（说明有children），需要往该内层id里建个children并push对应的数组；
        if(ele[idName] === parentId){
          if(!ele[childrenName]){
            ele[childrenName] = [];
          }
          ele[childrenName].push(element)
        }
      });
    }
  });
  // 过滤，按树展开，将多余的数组剔除
  return  list.filter(ele => !ele[parentIdName]);
}

export default util
